import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

class Message{
    public String from;
    public String to;
    public String message;
}
@WebServlet("/message")
public class MessageServlet extends HttpServlet {
    private ObjectMapper objectMapper=new ObjectMapper();
    //要把保存形式改成数据库，就不要list了
    //private List<Message>messages=new ArrayList<>();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //处理提交消息请求
        //获取
        Message message=objectMapper.readValue(req.getInputStream(),Message.class);
        //保存
        //messages.add(message);
        //通过contentType来告知页面，返回的数据是json格式
        //有这个声明
        save(message);
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write("{ \"ok\":true }");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取到消息列表，只要把消息列表中的客户端返回即可
        //使用objectMapper把java对象转成json格式的字符串
        List<Message>messages=load();
        String jsonString=objectMapper.writeValueAsString(messages);
        System.out.println("jsonString"+jsonString);
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(jsonString);
    }

    private void save(Message message){
        //把消息保存到数据库中
        //1.和数据库建立连接
        Connection connection=null;
        PreparedStatement statement=null;
        try {
            connection=DBUtil.getConnection();
            //2.构造SQL
            String sql="insert into message values(?,?,?)";
            statement=connection.prepareStatement(sql);
            statement.setString(1,message.from);
            statement.setString(2,message.to);
            statement.setString(3,message.message);
            //3.执行sql
            statement.executeUpdate();

        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,statement,null);
        }
    }


    private List<Message> load(){
        //从数据库中获取到所有的消息
        List<Message>messages=new ArrayList<>();
        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet resultSet=null;
        try {
            connection=DBUtil.getConnection();
            String sql="select * from message";
            statement=connection.prepareStatement(sql);
            statement.executeQuery();
            resultSet=statement.executeQuery();
            while (resultSet.next()){
                Message message=new Message();
                message.from=resultSet.getNString("from");
                message.to=resultSet.getNString("to");
                message.message=resultSet.getNString("message");
                messages.add(message);
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return messages;
    }

}
